package com.szrt.department.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.szrt.department.dao.DepartmentDao;
import com.szrt.department.entity.DepartmentEntity;
import com.szrt.utils.ConnectionUtil;

public class DepartmentDaoImpl implements DepartmentDao {

	@Override
	public int add(DepartmentEntity entity) {
		
		//1 得到数据库连接
		Connection connection = ConnectionUtil.getConnection();
		
		String sql="insert into jsp_department_t values(?,?)";
		int counter =0;
		PreparedStatement  statement = null;
		try {
			statement =connection.prepareStatement(sql);
			statement.setInt(1, entity.getDepartmentCode());
			statement.setString(2, entity.getDepartmentName());
			counter =statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(null != statement){
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(null != connection){
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
		}
		return counter;
	}

	@Override
	public int delete(Integer departmentCode) {
		
		String sql="DELETE  FROM jsp_department_t where DEPARTMENT_CODE=?";
		
		//1 获得连接
		Connection         connection = ConnectionUtil.getConnection();
		PreparedStatement  statement = null;
		ResultSet          set = null;
		int counter=-1;
		try {
			
			//2得到执行语句  带sql
			statement =connection.prepareStatement(sql);
			
			
			//3 把问号中的东西设置好
			statement.setInt(1, departmentCode);
			
			//4执行查询
			counter =statement.executeUpdate();
			
			return counter;
		} catch (SQLException e) {
			e.printStackTrace();
			return counter;
		}finally{
			if(null != set){
				try {
					set.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
			if(null != statement){
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
			if(null != connection){
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
		}
	}

	/**
	 * 方法设计中的关键：如果有可能涉及到多个条件 
	 * 
	 * >=5 就一定要封装成对象  面对对象
	 * 检出部门名称 或者 部门编码是否重复
	 * @param entity
	 * @return true 代表有重复的数据  false代表没有重复的数据
	 */
	@Override
	public int check(DepartmentEntity entity) {
		
		String sql="SELECT COUNT(1) AS COUNTER FROM jsp_department_t "
				+ " where DEPARTMENT_CODE=? OR DEPARTMENT_NAME=?";
		
		//1 获得连接
		Connection         connection = ConnectionUtil.getConnection();
		PreparedStatement  statement = null;
		ResultSet          set = null;
		int counter=-1;
		try {
			
			//2得到执行语句  带sql
			statement =connection.prepareStatement(sql);
			
			
			//3 把问号中的东西设置好
			statement.setInt(1, entity.getDepartmentCode());
			statement.setString(2, entity.getDepartmentName());
			
			//4执行查询
			set =statement.executeQuery();
			
			//5 之所以可以用if是因为我100%的肯定这个结果集里面只有一条数据
			if(set.next()){
				counter = set.getInt("COUNTER");
			}
			return counter;
		} catch (SQLException e) {
			e.printStackTrace();
			return counter;
		}finally{
			if(null != set){
				try {
					set.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
			if(null != statement){
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
			if(null != connection){
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
		}
		
	}


}
